package logic

import (
	"context"
	"time"

	"mall/common/enumx"
	"mall/common/resultx"
	"mall/service/user/model"
	"mall/service/user/rpc/internal/svc"
	"mall/service/user/rpc/types/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddUserTransactionLogLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddUserTransactionLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddUserTransactionLogLogic {
	return &AddUserTransactionLogLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddUserTransactionLogLogic) AddUserTransactionLog(in *user.AddUserTransactionLogRequest) (*user.AddUserTransactionLogResponse, error) {
	now := time.Now()
	log := &model.UserTransactionLog{
		UserID:   uint64(in.UserId),
		UserName: in.UserName,
		AssetType: uint8(in.AssetType),
		ChangeType: uint8(in.ChangeType),
		ChangeAmount: in.ChangeAmount,
		SourceType: uint8(in.SourceType),
		SourceID: uint64(in.SourceId),
		Desc: in.SourceDesc,
		Status: uint8(enumx.ObjectStatusAproved),
		CreatedAt: now,
		UpdatedAt: now,
	}
	err := l.svcCtx.User.InsertTransactionLog(l.ctx, log)
	if err != nil {
		return nil, resultx.StatusError(resultx.DB_ERROR, "")
	}

	return &user.AddUserTransactionLogResponse{}, nil
}
